#pragma once
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>
#include "Log.hpp"


using namespace LogModule;

class InetAddr
{
    public:
    InetAddr(struct sockaddr_in addr)
        :_addr(addr)
    {
        _ip = inet_ntoa(_addr.sin_addr);
        _port = ntohs(_addr.sin_port);
    }

    InetAddr(uint16_t port)
        :_port(port)
    {
        //网络转主机
        bzero(&_addr , sizeof(_addr));
        _addr.sin_addr.s_addr = INADDR_ANY;
        _addr.sin_port = port;
        _addr.sin_family = AF_INET;
        _ip = "0";
    }

    InetAddr(const std::string& ip , uint16_t port)
        :_ip(ip)
        ,_port(port)
    {
        //主机转网络
        bzero(&_addr , sizeof(_addr));
        _addr.sin_family = AF_INET;
        inet_aton(_ip.c_str() , &_addr.sin_addr);
        _addr.sin_port = port;
    }
    bool operator==(const InetAddr& addr)
    {
        return addr._ip == _ip;
    }

    struct sockaddr_in& NetAddr()
    {
        return _addr;
    }

    std::string GetIP()
    {
        return _ip;
    }

    int GetPort()
    {
        return _port;
    }
    ~InetAddr()
    {

    }
    private:
    struct sockaddr_in _addr;
    std::string _ip;
    int _port;
};